package com.example.zengyi;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class PrimeNumberHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "primes.db";
    private static final int DATABASE_VERSION = 1;
    public static final String TABLE_PRIMES = "primes";
    public static final String COLUMN_PRIME = "prime";

    public PrimeNumberHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE_PRIMES = "CREATE TABLE " + TABLE_PRIMES + "("
                + COLUMN_PRIME + " INTEGER PRIMARY KEY)";
        db.execSQL(CREATE_TABLE_PRIMES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRIMES);
        onCreate(db);
    }

    public void insertPrime(int prime) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRIME, prime);
        db.insert(TABLE_PRIMES, null, values);
        db.close();
    }

    public Cursor getAllPrimes() {
        SQLiteDatabase db = this.getReadableDatabase();
        return db.query(TABLE_PRIMES, new String[]{COLUMN_PRIME}, null, null, null, null, null);
    }
}